Skip to content

ZoomAnimation

Base classes

Name Children Inherits
ParallelAnimationGroup
prettyqt.core.parallelanimationgroup
Parallel group of animations.

⋔ Inheritance diagram

graph TD
  3735483353["animations.ZoomAnimation"]
  952010150["core.ParallelAnimationGroup"]
  3440267878["core.AnimationGroupMixin"]
  565069530["core.AbstractAnimationMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  844356195["QtCore.QParallelAnimationGroup"]
  1733551884["QtCore.QAnimationGroup"]
  2133980439["QtCore.QAbstractAnimation"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  952010150 --> 3735483353
  3440267878 --> 952010150
  565069530 --> 3440267878
  3227531497 --> 565069530
  2751680898 --> 3227531497
  2751680898 --> 565069530
  2751680898 --> 3440267878
  844356195 --> 952010150
  1733551884 --> 844356195
  2133980439 --> 1733551884
  3984060476 --> 2133980439
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ParallelAnimationGroup

Source code in prettyqt/animations/zoomanimation.py
class ZoomAnimation(core.ParallelAnimationGroup):
    ID = "zoom"

    def _widget(self) -> widgets.QWidget:
        return cast("widgets.QWidget", self.parent() or self.anim1.targetObject())

    def __init__(
        self,
        duration: int = 1000,
        start: float = 1.0,
        end: float = 1.0,
        easing: core.easingcurve.TypeStr = "in_out_sine",
        anchor: AnchorStr = "center",
        parent: core.QObject | None = None,
    ):
        self._start = start
        self._end = end
        self._anchor = anchor
        super().__init__(parent)
        # size change animation
        self.anim1 = core.PropertyAnimation()
        # slide animation
        self.anim2 = core.PropertyAnimation()
        if parent is not None:
            self.apply_to(cast("widgets.QWidget", parent))
            self.set_start_value(start)
            self.set_end_value(end)
        self.set_easing(easing)
        self.set_duration(duration)
        self.addAnimation(self.anim1)
        self.addAnimation(self.anim2)

    def set_start_value(self, factor: float):
        size = self._widget().size()
        self._start = factor
        self.anim1.setStartValue(size)
        self.anim2.setStartValue(self._widget().pos())

    def set_end_value(self, factor: float):
        self._end = factor
        zoom_ratio = self._end / self._start
        # setup zoom
        size = self._widget().size()
        new = size.toSizeF() * zoom_ratio
        self.anim1.setEndValue(new.toSize())
        # setup move
        move_x = -(zoom_ratio - 1) * self._widget().width()
        move_y = -(zoom_ratio - 1) * self._widget().height()
        match self._anchor:
            case "center":
                movement = core.Point(int(move_x / 2), int(move_y / 2))
            case "left":
                movement = core.Point(0, int(move_y / 2))
            case "right":
                movement = core.Point(int(move_x), int(move_y / 2))
            case "top":
                movement = core.Point(int(move_x / 2), 0)
            case "bottom":
                movement = core.Point(int(move_x / 2), int(move_y))
        self.anim2.setEndValue(self._widget().pos() + movement)

    def set_duration(self, duration: int):
        self.anim1.setDuration(duration)
        self.anim2.setDuration(duration)

    def set_easing(self, easing: core.easingcurve.TypeStr | core.QEasingCurve.Type):
        self.anim1.set_easing(easing)
        self.anim2.set_easing(easing)

    def apply_to(self, obj: widgets.QWidget):
        self.anim1.apply_to(obj.size)
        self.anim2.apply_to(obj.pos)

    def start(self, *args, **kwargs):
        # update values before starting
        self.set_start_value(self._start)
        self.set_end_value(self._end)
        super().start(*args, **kwargs)

:fa-brands-github: Show source on GitHub